package com.example.dreamlist2.me;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.dreamlist2.R;
import com.example.dreamlist2.StateInfo;
import com.example.dreamlist2.User;
import com.example.dreamlist2.Utils;
import com.timmy.tdialog.TDialog;
import com.timmy.tdialog.base.BindViewHolder;
import com.timmy.tdialog.listener.OnBindViewListener;
import com.timmy.tdialog.listener.OnViewClickListener;

import org.jetbrains.annotations.NotNull;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;


public class MeFragment extends Fragment {
    protected Activity mActivity;

    TextView uname, uid;
    LinearLayout change_uname, change_pswd, see_invite, info, logout;
    String Uinvite;
    StateInfo stateInfo;

    @Override
    public void onAttach(@NotNull Context context) {
        super.onAttach(context);
        this.mActivity = (Activity)context;
    }

    //StateInfo stateInfo = (StateInfo) (mActivity.getApplication());


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.me_main, container, false);

        uname = view.findViewById(R.id.user_name);
        uid = view.findViewById(R.id.user_id);
        change_uname = view.findViewById(R.id.change_uname);
        change_pswd = view.findViewById(R.id.change_pswd);
        see_invite = view.findViewById(R.id.show_uinvited);
        info = view.findViewById(R.id.info);
        logout = view.findViewById(R.id.logout);
        stateInfo = (StateInfo) mActivity.getApplication();
        Uinvite = stateInfo.getUserin().getUinvite();

        //Bundle bundle = getArguments();
        //Uid = bundle.getString("uid");

        //设定用户名
        basic_settings();

        change_uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TDialog.Builder(getChildFragmentManager())
                        .setLayoutRes(R.layout.duname)    //设置弹窗展示的xml布局
                        .setScreenWidthAspect(mActivity, 0.8f)   //设置弹窗宽度(参数aspect为屏幕宽度比例 0 - 1f)
                        .setScreenHeightAspect(mActivity, 0.3f)  //设置弹窗高度(参数aspect为屏幕宽度比例 0 - 1f)
                        .setOnBindViewListener(new OnBindViewListener() {
                            @Override
                            public void bindView(BindViewHolder viewHolder) {
                                //动态改变view的内容，暂不需要
                            }
                        })
                        .setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialog) {
                            }
                        })
                        .addOnClickListener(R.id.ok,R.id.cancel)
                        .setOnViewClickListener(new OnViewClickListener() {
                            @Override
                            public void onViewClick(BindViewHolder viewHolder, View view, TDialog tDialog) {
                                switch (view.getId()){
                                    case R.id.ok:
                                        //todo:调用修改用户名函数
                                        register_nuname(viewHolder);
                                        basic_settings();
                                        tDialog.dismiss();
                                        break;
                                    case R.id.cancel:
                                        tDialog.dismiss();
                                        Toast.makeText(mActivity, "取消", Toast.LENGTH_SHORT).show();
                                        break;
                                }
                            }
                        })
                        .create()   //创建TDialog
                        .show();    //展示
            }
        });

        change_pswd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TDialog.Builder(getChildFragmentManager())
                        .setLayoutRes(R.layout.dupswd)    //设置弹窗展示的xml布局
                        .setScreenWidthAspect(mActivity, 0.8f)   //设置弹窗宽度(参数aspect为屏幕宽度比例 0 - 1f)
                        .setScreenHeightAspect(mActivity, 0.3f)  //设置弹窗高度(参数aspect为屏幕宽度比例 0 - 1f)
                        .setOnBindViewListener(new OnBindViewListener() {
                            @Override
                            public void bindView(BindViewHolder viewHolder) {
                                //动态改变view的内容，暂不需要
                            }
                        })
                        .setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialog) {
                            }
                        })
                        .addOnClickListener(R.id.pok,R.id.pcancel)
                        .setOnViewClickListener(new OnViewClickListener() {
                            @Override
                            public void onViewClick(BindViewHolder viewHolder, View view, TDialog tDialog) {
                                switch (view.getId()){
                                    case R.id.pok:
                                        //todo:调用修改密码函数
                                        register_npswd(viewHolder);
                                        tDialog.dismiss();
                                        basic_settings();
                                        break;
                                    case R.id.pcancel:
                                        Toast.makeText(mActivity, "取消", Toast.LENGTH_SHORT).show();
                                        tDialog.dismiss();
                                        break;
                                }
                            }
                        })
                        .create()   //创建TDialog
                        .show();    //展示
            }
        });

        info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TDialog.Builder(getChildFragmentManager())
                        .setLayoutRes(R.layout.dinfo)    //设置弹窗展示的xml布局
                        .setScreenWidthAspect(mActivity, 0.8f)   //设置弹窗宽度(参数aspect为屏幕宽度比例 0 - 1f)
                        .setScreenHeightAspect(mActivity, 0.8f)  //设置弹窗高度(参数aspect为屏幕宽度比例 0 - 1f)
                        .setOnBindViewListener(new OnBindViewListener() {
                            @Override
                            public void bindView(BindViewHolder viewHolder) {
                                //动态改变view的内容，暂不需要
                            }
                        })
                        .setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialog) {
                                Toast.makeText(mActivity, "确认", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .addOnClickListener(R.id.iok)
                        .setOnViewClickListener(new OnViewClickListener() {
                            @Override
                            public void onViewClick(BindViewHolder viewHolder, View view, TDialog tDialog) {
                                switch (view.getId()){
                                    case R.id.iok:
                                        //todo:显示信息
                                        tDialog.dismiss();
                                        break;
                                }
                            }
                        })
                        .create()   //创建TDialog
                        .show();    //展示
            }
        });

        see_invite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TDialog.Builder(getChildFragmentManager())
                        .setLayoutRes(R.layout.dinvite)    //设置弹窗展示的xml布局
                        .setScreenWidthAspect(mActivity, 0.7f)   //设置弹窗宽度(参数aspect为屏幕宽度比例 0 - 1f)
                        .setScreenHeightAspect(mActivity, 0.5f)  //设置弹窗高度(参数aspect为屏幕宽度比例 0 - 1f)
                        .setOnBindViewListener(new OnBindViewListener() {
                            @Override
                            public void bindView(BindViewHolder viewHolder) {
                                TextView invite = viewHolder.getView(R.id.invite_code);
                                invite.setText(Uinvite);
                            }
                        })
                        .setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialog) {
                                Toast.makeText(mActivity, "确认", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .addOnClickListener(R.id.invite_ok)
                        .setOnViewClickListener(new OnViewClickListener() {
                            @Override
                            public void onViewClick(BindViewHolder viewHolder, View view, TDialog tDialog) {
                                switch (view.getId()){
                                    case R.id.invite_ok:
                                        //todo:显示信息
                                        tDialog.dismiss();
                                        break;
                                }
                            }
                        })
                        .create()   //创建TDialog
                        .show();    //展示
            }
        });

        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stateInfo.setLogin(false);
                mActivity.finish();
            }
        });

        return view;
    }

    private void basic_settings(){
        uname.setText(stateInfo.getUserin().getDisplayName());
        uid.setText(stateInfo.getUserin().getObjectId());
    }

    private void register_nuname(BindViewHolder view){
        EditText new_uname = view.getView(R.id.new_uname);
        String suname = new_uname.getText().toString();
        User user = stateInfo.getUserin();
        user.setUname(suname);
        user.update(user.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    Log.i("bmob","更新成功");
                    stateInfo.setUserin(user);
                    Toast.makeText(mActivity, "修改成功", Toast.LENGTH_SHORT).show();
                }else{
                    Log.i("bmob","更新失败："+e.getMessage()+","+e.getErrorCode());
                    Toast.makeText(mActivity, "修改失败，请重试", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void register_npswd(BindViewHolder view){
        EditText new_pswd = view.getView(R.id.new_pswd);
        EditText new_pswd2 = view.getView(R.id.new_pswd2);

        String snpswd = new_pswd.getText().toString();
        String snpswd2 = new_pswd2.getText().toString();

        if (snpswd.length() < 7){
            Toast.makeText(mActivity,"密码至少6位",Toast.LENGTH_SHORT).show();
            return;
        }
        if (!snpswd.equals(snpswd2)){
            Toast.makeText(mActivity,"两次密码不相同", Toast.LENGTH_SHORT).show();
            return;
        }

        User user = stateInfo.getUserin();
        user.setPassword(snpswd);

        user.update(user.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    Log.i("bmob","更新成功");
                    Toast.makeText(mActivity, "修改成功", Toast.LENGTH_SHORT).show();
                    stateInfo.setUserin(user);
                }else{
                    Log.i("bmob","更新失败："+e.getMessage()+","+e.getErrorCode());
                    Toast.makeText(mActivity, "修改失败，请重试", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}